/*
Copyright [2023] [fuwen_soft]
*/
package vip.xiaonuo.modular.xylogytemplate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import vip.xiaonuo.core.consts.CommonConstant;
import vip.xiaonuo.core.enums.CommonStatusEnum;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.PoiUtil;
import vip.xiaonuo.modular.xylogytemplate.entity.XyLogyTemplate;
import vip.xiaonuo.modular.xylogytemplate.enums.XyLogyTemplateExceptionEnum;
import vip.xiaonuo.modular.xylogytemplate.mapper.XyLogyTemplateMapper;
import vip.xiaonuo.modular.xylogytemplate.param.XyLogyTemplateParam;
import vip.xiaonuo.modular.xylogytemplate.service.XyLogyTemplateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * 谱书模板service接口实现类
 *
 * @author yanwei
 * @date 2023-05-05 15:44:28
 */
@Service
public class XyLogyTemplateServiceImpl extends ServiceImpl<XyLogyTemplateMapper, XyLogyTemplate> implements XyLogyTemplateService {

    @Override
    public PageResult<XyLogyTemplate> page(XyLogyTemplateParam xyLogyTemplateParam) {
        QueryWrapper<XyLogyTemplate> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(xyLogyTemplateParam)) {

            // 根据版型名称 查询
            if (ObjectUtil.isNotEmpty(xyLogyTemplateParam.getTemplateName())) {
                queryWrapper.lambda().eq(XyLogyTemplate::getTemplateName, xyLogyTemplateParam.getTemplateName());
            }
            // 根据版型封面图 查询
            if (ObjectUtil.isNotEmpty(xyLogyTemplateParam.getTemplatePic())) {
                queryWrapper.lambda().eq(XyLogyTemplate::getTemplatePic, xyLogyTemplateParam.getTemplatePic());
            }
            // 根据版型备注 查询
            if (ObjectUtil.isNotEmpty(xyLogyTemplateParam.getReBack())) {
                queryWrapper.lambda().eq(XyLogyTemplate::getReBack, xyLogyTemplateParam.getReBack());
            }
            // 根据版型链接 查询
            if (ObjectUtil.isNotEmpty(xyLogyTemplateParam.getTemLink())) {
                queryWrapper.lambda().eq(XyLogyTemplate::getTemLink, xyLogyTemplateParam.getTemLink());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<XyLogyTemplate> list(XyLogyTemplateParam xyLogyTemplateParam) {
        return this.list();
    }

    @Override
    public void add(XyLogyTemplateParam xyLogyTemplateParam) {
        XyLogyTemplate xyLogyTemplate = new XyLogyTemplate();
        BeanUtil.copyProperties(xyLogyTemplateParam, xyLogyTemplate);
        this.save(xyLogyTemplate);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<XyLogyTemplateParam> xyLogyTemplateParamList) {
        xyLogyTemplateParamList.forEach(xyLogyTemplateParam -> {
            this.removeById(xyLogyTemplateParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(XyLogyTemplateParam xyLogyTemplateParam) {
        XyLogyTemplate xyLogyTemplate = this.queryXyLogyTemplate(xyLogyTemplateParam);
        BeanUtil.copyProperties(xyLogyTemplateParam, xyLogyTemplate);
        this.updateById(xyLogyTemplate);
    }

    @Override
    public XyLogyTemplate detail(XyLogyTemplateParam xyLogyTemplateParam) {
        return this.queryXyLogyTemplate(xyLogyTemplateParam);
    }

    /**
     * 获取谱书模板
     *
     * @author yanwei
     * @date 2023-05-05 15:44:28
     */
    private XyLogyTemplate queryXyLogyTemplate(XyLogyTemplateParam xyLogyTemplateParam) {
        XyLogyTemplate xyLogyTemplate = this.getById(xyLogyTemplateParam.getId());
        if (ObjectUtil.isNull(xyLogyTemplate)) {
            throw new ServiceException(XyLogyTemplateExceptionEnum.NOT_EXIST);
        }
        return xyLogyTemplate;
    }

    @Override
    public void export(XyLogyTemplateParam xyLogyTemplateParam) {
        List<XyLogyTemplate> list = this.list(xyLogyTemplateParam);
        PoiUtil.exportExcelWithStream("SnowyXyLogyTemplate.xls", XyLogyTemplate.class, list);
    }

}
